package com.ktwlsoft.fundmanage.service;

import com.google.common.collect.Lists;
import com.ktwlsoft.fundmanage.domain.entity.GroupDuty;
import com.ktwlsoft.fundmanage.domain.repository.GroupDutyRepository;
import com.ktwlsoft.fundmanage.service.FInterface.ICandidate;
import com.ktwlsoft.fundmanage.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * CandidateImpl
 *
 * @author Name on 2019/9/5 15:59
 * @version 1.0
 */
@Service("candidateService")
public class CandidateService implements ICandidate {

    @Autowired
    private GroupDutyRepository groupDutyRepository;

    /**
     * 获取业务员
     *
     * @return List<String>
     */
    @Override
    public List<String> getClerk(){
        return getRoleNameList("CLERK");
    }

    @Override
    public List<String> getClerkAndAccountant(){
        List<String> result = getRoleNameList("CLERK");
        result.addAll(getAccountant());
        return result;
    }

    @Override
    public List<String> getAuditorAndCashier(){
        List<String> result = getAuditor();
        result.addAll(getCashier());
        return result;
    }

    @Override
    public List<String> getCashierAndAccount(){
        List<String> result = getCashier();
        result.addAll(getAccountant());
        return result;
    }

    /**
     * 获取会计
     *
     * @return List<String>
     */
    @Override
    public List<String> getAccountant(){
        return getRoleNameList("ACCOUNTANT");
    }

    /**
     * 获取审核
     *
     * @return List<String>
     */
    @Override
    public List<String> getAuditor(){
        return getRoleNameList("AUDITOR");
    }

    /**
     * 获取出纳
     *
     * @return List<String>
     */
    @Override
    public List<String> getCashier(){
        return getRoleNameList("CASHIER");
    }

    @Override
    public boolean checkWhetherClerk(){
        List<String> clerks = this.getClerk();
        List<String> roles = UserUtils.getUser().getRoles();
        return clerks.retainAll(roles);
    }

    /**
      * 获取角色名集合 on 2019/9/5 17:35
      *
      * @param userTask userTask
      * @return List<String>
      */
    private List<String> getRoleNameList(String userTask){
        List<String> strings = Lists.newArrayList();
        List<GroupDuty> groupDuties = groupDutyRepository.findByUserTask(userTask);
        if(groupDuties == null || groupDuties.size() <= 0){
            return strings;
        }
        groupDuties.forEach(value -> {
            if (value.getGroupName() != null && !"".equals(value.getGroupName().trim())) {
                strings.addAll(Arrays.asList(value.getGroupName().split(",")));
            }
        });
        return strings;
    }
}